﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;

namespace Hotfix
{
    [MessageHandler(AppType.Map)]
    public class CreatRoomHandle : AMRpcHandler<C2M_CreatRoom, M2C_CreatRoom>
    {
        protected override async void Run(Session session, C2M_CreatRoom message, Action<M2C_CreatRoom> reply)
        {
            Log.Debug("CreatRoom:" + message.RoomName);
            M2C_CreatRoom response = new M2C_CreatRoom();
            try
            {
                RoomManagerComponent rm = Game.Scene.GetComponent<RoomManagerComponent>();
                Room r = rm.GetRoom(message.RoomName);
                if (r != null)
                {
                    response.Error = ErrorCode.ERR_RoomExist;
                    reply(response);
                    return;
                }
                Room room = EntityFactory.Create<Room>();
                //await room.AddComponent<ActorComponent>().AddLocation();
                room.RoomName = message.RoomName;
                room.AddComponent<RoomSessionComponent>().SessionIds.Add(session.Id);
                Log.Debug("CreatRoom,ID" + room.Id);

                rm.RoomList.Add(room);

                reply(response);
            }
            catch (Exception e)
            {
                ReplyError(response, e, reply);
            }

        }
    }
}
